/**
 * Copyright (C) 2007 The AsyncMail Group <asyncmail@googlegroups.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.asyncmail.smtp;

import java.util.LinkedList;
import java.util.List;

/**
 * Contains an SMTP result
 */
public final class SMTPResponseImpl implements SMTPResponse {

    private String retCode = null;

    private List<String> lines = null;

    private boolean endSession = false;

    public SMTPResponseImpl() {
        // TODO: Should we remove this constructur to force the developers to
        // specify all needed informations ?
    }

    /**
     * @see #SMTPResponseImpl(String, StringBuffer)
     */
    public SMTPResponseImpl(final String code, final String description) {
        if (code == null)
            throw new IllegalArgumentException(
                    "SMTPResponseImpl code can not be null");
        if (description == null)
            new IllegalArgumentException(
                    "SMTPResponseImpl description can not be null");

        this.setRetCode(code);
        this.appendLine(description);
    }

    /**
     * Construct a new SMTPResponseImpl. The given code and description can not be
     * null, if null an IllegalArgumentException get thrown
     * 
     * @param code
     *                the returnCode
     * @param description
     *                the description
     */
    public SMTPResponseImpl(final String code, final StringBuffer description) {
        if (code == null)
            throw new IllegalArgumentException(
                    "SMTPResponseImpl code can not be null");
        if (description == null)
            new IllegalArgumentException(
                    "SMTPResponseImpl description can not be null");

        this.setRetCode(code);
        this.appendLine(description);
    }

    /**
     * Append the responseLine to the SMTPResponseImpl
     * 
     * @param line
     *                the responseLine to append
     */
    public void appendLine(final String line) {
        if (lines == null) {
            lines = new LinkedList<String>();
        }
        lines.add(line);
    }

    /**
     * @see #appendLine(String)
     */
    public void appendLine(StringBuffer line) {
        appendLine(line.toString());
    }

    /* (non-Javadoc)
     * @see com.googlecode.asyncmail.smtp.SMTPResponse#getRetCode()
     */
    public String getRetCode() {
        return retCode;
    }

    /**
     * Set the SMTPCode
     * 
     * @param retCode
     *                the SMTPCode
     */
    public void setRetCode(final String retCode) {
        this.retCode = retCode;
    }

    /* (non-Javadoc)
     * @see com.googlecode.asyncmail.smtp.SMTPResponse#getLines()
     */
    public List<String> getLines() {
        return lines;
    }

    /* (non-Javadoc)
     * @see com.googlecode.asyncmail.smtp.SMTPResponse#isEndSession()
     */
    public boolean isEndSession() {
        return endSession;
    }

    /**
     * Set to true to end the session
     * 
     * @param endSession
     */
    public void setEndSession(final boolean endSession) {
        this.endSession = endSession;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return getRetCode() + " " + getLines();
    }
}
